package labox.innovation.gameserver.datatables;

import java.io.File;
import java.io.FileInputStream;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javolution.util.FastMap;
import labox.innovation.config.Config;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;

/**
 * 获取维修系数配置信息类
 * @author Frank
 *
 */
public class Repair {
	private static final Logger _log = LoggerFactory.getLogger(Repair.class.getName());

	private static final Repair _instance = new Repair();

	private final String[] keys = { "LevelRate", "LowLevel", "HighLevel", "Talisman", "BaoDevice", "LingDevice", "Treasure", "Horl" };

	private FastMap<String, String> _lists;

//	private static long timer;

	private Repair() {
		loadConfig();
	}

	/**
	 * 载入维修系数配置信息
	 */
	private void loadConfig() {
	    Node node;
	    
		try {
			File f = new File(Config.DATAPACK_ROOT, "/data/repairCoefficient.xml");
			DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
			DocumentBuilder builder = factory.newDocumentBuilder();
			Document document = builder.parse(new FileInputStream(f));
			Element root;
			root = document.getDocumentElement();

			if (root.getTagName().compareTo("Repair") != 0) {
				throw new Exception("Not a repair file!");
			}

			if (_lists == null)
				_lists = new FastMap<String, String>();
			String key = "";
			String value = "";
			for (int i = 0; i < keys.length; i++) {
				node = root.getElementsByTagName(keys[i]).item(0);
				
				if (node != null) {
					key = node.getNodeName();
					value = node.getTextContent();
					_lists.put(key, value);
				}
			}
		} catch (Exception e) {
			_log.error("读取修理费用配置信息文件出错", Config.SERVER_ID, e);
		}

//		timer = System.currentTimeMillis();
	}

	/**
	 * 维修系数获取类单利获取方法
	 * @param isNeedRefresh  是否需要重新载入配置信息
	 * @return
	 */
	public static Repair getInstance() {
//		if ((System.currentTimeMillis() - timer) > 60 * 1000) { //-- 超过1分钟刷新一次
//			_instance.loadConfig();
//		}

		return _instance;
	}

	/**
	 * 根据key获取value值
	 * @param key
	 * @return
	 */
	public String getValueByKey(String key) {
		return _lists.get(key);
	}
}
